//% color="#ff9f06" iconWidth=50 iconHeight=40
namespace RFID_Module{
    
    //% block="Initialize RFID module [VER]" blockType="command"
    //% VER.shadow="dropdown" VER.options="VER"
    export function RFID_init(parameter: any, block: any) { 
        let ver=parameter.VER.code;

        if (ver == "V1"){

            Generator.addImport('from pinpong.board import Board,Pin');
            Generator.addImport('from dfrobot_pn7150 import *');
            Generator.addInit(`board_begin`,`Board().begin()`);
            Generator.addInit(`RFID_object`,`RFID = DFRobot_PN7150(i2c_addr=PN7150_I2C_ADDR, bus_num=4)`);
            Generator.addInit(`RFID_connect`,`
while (RFID.connect() == False):
    print("init fail!")
    time.sleep(1)
print("successful connection.")
`);
            Generator.addInit(`RFID_mode`,`RFID.mode_rw()`);

        }
        else {

            Generator.addImport('from pinpong.board import Board,Pin');
            Generator.addImport('from dfrobot_fm17550 import Rfid');
            Generator.addImport('import time');
            Generator.addInit(`board_begin`,`Board().begin()`);
            Generator.addInit(`RFID_object`,`RFID = Rfid()`);
            Generator.addInit(`RFID_connect`,`
while (RFID.begin() == False):
    print("init fail!")
    time.sleep(1)
print("successful connection.")
`); 

        }

    }



    //% block="Write [DATA] to byte [BYTE_NUM] of RFID block [BLOCK_NUM]" blockType="command" 
    //% DATA.shadow="range" DATA.params.min=0 DATA.params.max=255 DATA.defl=200
    //% BYTE_NUM.shadow="dropdown" BYTE_NUM.options="BYTE_NUM"
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    export function RFID_write(parameter: any, block: any) { 
        let data=parameter.DATA.code;
        let block_num=parameter.BLOCK_NUM.code;
        let byte_num=parameter.BYTE_NUM.code;

        Generator.addCode(`RFID.write_index_data(${block_num}, ${byte_num}, ${data})`);
    }



    //% block="Read all data from block [BLOCK_NUM] of the RFID module(string)" blockType="reporter" 
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    export function RFID_read_all(parameter: any, block: any) { 
        let block_num=parameter.BLOCK_NUM.code;

        Generator.addCode(`RFID.read_data(${block_num})`);
    }



    //% block="Read byte [BYTE_NUM] of RFID block [BLOCK_NUM]" blockType="reporter" 
    //% BLOCK_NUM.shadow="dropdown" BLOCK_NUM.options="BLOCK_NUM"
    //% BYTE_NUM.shadow="dropdown" BYTE_NUM.options="BYTE_NUM"
    export function RFID_read(parameter: any, block: any) { 
        let block_num=parameter.BLOCK_NUM.code;
        let byte_num=parameter.BYTE_NUM.code;

        Generator.addCode(`RFID.read_data(${block_num}, ${byte_num})`);
    }



    //% block="Read the UID of the tag card" blockType="reporter" 
    export function RFID_read_UID(parameter: any, block: any) { 

        Generator.addCode(`RFID.read_uid()`);
    }


    //% block="Detected card. UID:[UID]?" blockType="boolean" 
    //% UID.shadow="string" UID.defl="acee50e3"
    export function RFID_SCAN_UID(parameter: any, block: any) { 
        
        let uid=parameter.UID.code;
        Generator.addCode(`RFID.scan(${uid})`);
    }


    //% block="Detected card?" blockType="boolean" 
    export function RFID_SCAN(parameter: any, block: any) { 

        Generator.addCode(`RFID.scan()`);
    }

}

